package com.blueocean_health.care.management.controller;


import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.OrderNurseDailyReport;
import com.blueocean_health.care.management.domain.ordervo.OrderNurseDailyVo;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.service.ReportService;

/**
 * @author			作者:            zwg.BlueOcean
 * @date       			创建时间：2017年8月30日  下午2:57:43
 * @version  		版本号：    1.0*
 * @parameter  	* 
 * @since  			* 
 * @return  			*
 * @description	 查询护工日报表	
 */
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderNurseDailyReportController {
	@Autowired
	private ReportService reportService;
	@RequestMapping("/web/nurse/daily-report")
	public SysResult query(HttpServletRequest request,@RequestBody OrderNurseDailyVo qvo) throws Exception{
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId=courrentUser.getHospitalId();
		//数据处理
		//校验日期格式
		if(StringUtils.isEmpty(qvo.getDate())){
			qvo.setDate(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
		}
		if( !qvo.getDate().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
			throw new Exception("查询日期不符合规范{yyyy-MM-dd}");
		}
		return reportService.getNurseDailyReport(qvo,hospitalId);
	}
	
	/**
	 * 导出excel
	 * @param request
	 * @param qvo
	 * @param response
	 * @throws Exception
	 */
	//@RequestBody OrderNurseDailyVo qvo
	@RequestMapping("/web/nurse/daily-report/excel")
	public void getNurseDailyReportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
			String hospitalId=courrentUser.getHospitalId();
			OrderNurseDailyVo qvo=new OrderNurseDailyVo();
			qvo.setDate(request.getParameter("date"));
			String dept=request.getParameter("department");
			qvo.setDepartment(StringUtils.isEmpty(dept)?null:Integer.valueOf(dept));
			if(StringUtils.isEmpty(qvo.getDate())){
				qvo.setDate(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
			}
			if(!qvo.getDate().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
				throw new Exception("查询日期不符合规范{yyyy-MM-dd}");
			}
			List<OrderNurseDailyReport> results = reportService.findNurseDailyListData(qvo, hospitalId);
			String finalFileName = URLEncoder.encode("护工日报表.xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, results, OrderNurseDailyReport.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}

}

